package com.example.moblie2021606143cwj0407homework;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private Button btnReset;
    private TextView tvMainQuestion1;
    private RadioGroup radioGroup1;
    private RadioButton radbtn1A;
    private RadioButton radbtn1B;
    private RadioButton radbtn1C;
    private RadioButton radbtn1D;
    private TextView tvMainQuestion2;
    private RadioGroup radioGroup2;
    private RadioButton radbtn2A;
    private RadioButton radbtn2B;
    private RadioButton radbtn2C;
    private RadioButton radbtn2D;
    private TextView tvMainQuestion3;
    private RadioGroup radioGroup3;
    private RadioButton radbtn3A;
    private RadioButton radbtn3B;
    private RadioButton radbtn3C;
    private RadioButton radbtn3D;
    private TextView tvMainQuestion4;
    private RadioGroup radioGroup4;
    private RadioButton radbtn4A;
    private RadioButton radbtn4B;
    private RadioButton radbtn4C;
    private RadioButton radbtn4D;
    private Button btnSubmit;
    private TextView tvMainGrade;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radbtn = findViewById(checkedId);
                radbtn.getText().toString();
                Log.i("RadioButton", radbtn.getText().toString());
            }
        });
        radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radbtn = findViewById(checkedId);
                radbtn.getText().toString();
                Log.i("RadioButton", radbtn.getText().toString());
            }
        });
        radioGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radbtn = findViewById(checkedId);
                radbtn.getText().toString();
                Log.i("RadioButton", radbtn.getText().toString());
            }
        });
        radioGroup4.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radbtn = findViewById(checkedId);
                radbtn.getText().toString();
                Log.i("RadioButton", radbtn.getText().toString());
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int optionID1 = radioGroup1.getCheckedRadioButtonId();
                int optionID2 = radioGroup2.getCheckedRadioButtonId();
                int optionID3 = radioGroup3.getCheckedRadioButtonId();
                int optionID4 = radioGroup4.getCheckedRadioButtonId();
                if (optionID1 == R.id.radbtn1A & optionID2 == R.id.radbtn2B & optionID3 == R.id.radbtn3B & optionID4 == R.id.radbtn4C) {
                    tvMainGrade.setText("分数：100分");
                } else if (optionID1 == R.id.radbtn1A & optionID2 == R.id.radbtn2C & optionID3 == R.id.radbtn3B || optionID2 == R.id.radbtn2C & optionID3 == R.id.radbtn3B & optionID4 == R.id.radbtn4A || optionID1 == R.id.radbtn1A & optionID3 == R.id.radbtn3B & optionID4 == R.id.radbtn4A) {
                    tvMainGrade.setText("分数：75分");
                } else if (optionID1 == R.id.radbtn1A & optionID2 == R.id.radbtn2D || optionID1 == R.id.radbtn3B & optionID3 == R.id.radbtn3B || optionID1 == R.id.radbtn1A & optionID4 == R.id.radbtn4A || optionID2 == R.id.radbtn2C & optionID3 == R.id.radbtn3B || optionID2 == R.id.radbtn2C & optionID4 == R.id.radbtn4A || optionID3 == R.id.radbtn3B & optionID4 == R.id.radbtn4A) {
                    tvMainGrade.setText("分数：50分");
                } else if (optionID1 == R.id.radbtn1A || optionID2 == R.id.radbtn2C || optionID3 == R.id.radbtn3B || optionID4 == R.id.radbtn4A) {
                    tvMainGrade.setText("分数：25分");
                } else if (optionID1 == -1 || optionID2 == -1 || optionID3 == -1 || optionID4 == -1) {
                    Toast.makeText(MainActivity.this, "请作出选择", Toast.LENGTH_SHORT).show();
                } else {
                    tvMainGrade.setText("分数：0分");
                }
            }
        });
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                radbtn1A.setChecked(false);
                radbtn1B.setChecked(false);
                radbtn1C.setChecked(false);
                radbtn1D.setChecked(false);
                radbtn2A.setChecked(false);
                radbtn2B.setChecked(false);
                radbtn2C.setChecked(false);
                radbtn2D.setChecked(false);
                radbtn3A.setChecked(false);
                radbtn3B.setChecked(false);
                radbtn3C.setChecked(false);
                radbtn3D.setChecked(false);
                radbtn4A.setChecked(false);
                radbtn4B.setChecked(false);
                radbtn4C.setChecked(false);
                radbtn4D.setChecked(false);
                tvMainGrade.setText("");
                Toast.makeText(MainActivity.this, "重置完成", Toast.LENGTH_SHORT).show();
            }
        });

    }
    private void initView() {
        tvMainTitle = findViewById(R.id.tv_main_title);
        btnReset = findViewById(R.id.btnReset);
        tvMainQuestion1 = findViewById(R.id.tv_main_question1);
        radioGroup1 = findViewById(R.id.radioGroup1);
        radbtn1A = findViewById(R.id.radbtn1A);
        radbtn1B = findViewById(R.id.radbtn1B);
        radbtn1C = findViewById(R.id.radbtn1C);
        radbtn1D = findViewById(R.id.radbtn1D);
        tvMainQuestion2 = findViewById(R.id.tv_main_question2);
        radioGroup2 = findViewById(R.id.radioGroup2);
        radbtn2A = findViewById(R.id.radbtn2A);
        radbtn2B = findViewById(R.id.radbtn2B);
        radbtn2C = findViewById(R.id.radbtn2C);
        radbtn2D = findViewById(R.id.radbtn2D);
        tvMainQuestion3 = findViewById(R.id.tv_main_question3);
        radioGroup3 = findViewById(R.id.radioGroup3);
        radbtn3A = findViewById(R.id.radbtn3A);
        radbtn3B = findViewById(R.id.radbtn3B);
        radbtn3C = findViewById(R.id.radbtn3C);
        radbtn3D = findViewById(R.id.radbtn3D);
        tvMainQuestion4 = findViewById(R.id.tv_main_question4);
        radioGroup4 = findViewById(R.id.radioGroup4);
        radbtn4A = findViewById(R.id.radbtn4A);
        radbtn4B = findViewById(R.id.radbtn4B);
        radbtn4C = findViewById(R.id.radbtn4C);
        radbtn4D = findViewById(R.id.radbtn4D);
        btnSubmit = findViewById(R.id.btn_submit);
        tvMainGrade = findViewById(R.id.tv_main_grade);
    }
}


